package org.jeecg.modules.htxincailiao.news.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RequestUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.news.entity.HTNews;
import org.jeecg.modules.htxincailiao.news.service.IHTNewsCategoryService;
import org.jeecg.modules.htxincailiao.news.service.IHTNewsService;
import org.jsoup.internal.StringUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: ht_news
 * @Author: jeecg-boot
 * @Date: 2021-08-11
 * @Version: V1.0
 */
@Api(tags = "ht_news")
@RestController
@RequestMapping("/news/hTNews")
@Slf4j
public class HTNewsController extends JeecgController<HTNews, IHTNewsService> {
    @Resource
    private IHTNewsService hTNewsService;

	@Resource
	private IHTNewsCategoryService hTNewsCategoryService;

    /**
     * 分页列表查询
     *
     * @param hTNews
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "ht_news-分页列表查询")
    @ApiOperation(value = "ht_news-分页列表查询", notes = "ht_news-分页列表查询")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> queryPageList(HTNews hTNews,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {


        if (req.getMethod().equals("POST")) {
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                hTNews = JSONObject.parseObject(line, HTNews.class);
                pageNo = oConvertUtils.isNotEmpty(hTNews.getPageNo()) ? hTNews.getPageNo() : pageNo;
                pageSize = oConvertUtils.isNotEmpty(hTNews.getPageSize()) ? hTNews.getPageSize() : pageSize;
            }
        }

        if (oConvertUtils.isNotEmpty(hTNews) &&
                oConvertUtils.isNotEmpty(hTNews.getCategoryId()) &&
                hTNews.getCategoryId().equals("0")) {
            //全部分类
            hTNews.setCategoryId(null);
        }

        QueryWrapper<HTNews> queryWrapper = QueryGenerator.initQueryWrapper(hTNews, req.getParameterMap());
        LambdaQueryWrapper<HTNews> lambdaQueryWrapper = queryWrapper.lambda();
        // 生产加工 场地租赁 检验检测 金融服务四类的资讯
        if (StringUtil.isBlank(hTNews.getDemandAll())) {
            lambdaQueryWrapper.isNull(HTNews::getDemandType);
        } else {
            if ("1".equals(hTNews.getDemandAll())) {
                //为1时查询全部
                lambdaQueryWrapper.isNotNull(HTNews::getDemandType);
            } else {
                lambdaQueryWrapper.eq(HTNews::getDemandType, hTNews.getDemandType());
            }
        }
//        queryWrapper.orderByDesc("updateTime");
//        queryWrapper.orderByDesc("update_time");
        lambdaQueryWrapper.orderByDesc(HTNews::getCreateTime);
        Page<HTNews> page = new Page<HTNews>(pageNo, pageSize);
        IPage<HTNews> pageList = hTNewsService.page(page, lambdaQueryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param hTNews
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "ht_news-分页列表查询")
    @ApiOperation(value = "ht_news-分页列表查询", notes = "ht_news-分页列表查询")
    @RequestMapping(value = "/pc-home-list", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> queryPCHomePageList(HTNews hTNews,
                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                         HttpServletRequest req) {
        if (req.getMethod().equals("POST")) {
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                hTNews = JSONObject.parseObject(line, HTNews.class);
                pageNo = oConvertUtils.isNotEmpty(hTNews.getPageNo()) ? hTNews.getPageNo() : pageNo;
                pageSize = oConvertUtils.isNotEmpty(hTNews.getPageSize()) ? hTNews.getPageSize() : pageSize;
            }
        }

        if (oConvertUtils.isNotEmpty(hTNews) &&
                oConvertUtils.isNotEmpty(hTNews.getCategoryId()) &&
                hTNews.getCategoryId().equals("0")) {
            //全部分类
            hTNews.setCategoryId(null);
        }


        QueryWrapper<HTNews> queryWrapper = QueryGenerator.initQueryWrapper(hTNews, req.getParameterMap());
//        queryWrapper.orderByDesc("update_time");
        queryWrapper.orderByDesc("is_home_recommended").orderByDesc("create_time");

        Page<HTNews> page = new Page<HTNews>(pageNo, pageSize);
        IPage<HTNews> pageList = hTNewsService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param hTNews
     * @return
     */
    @AutoLog(value = "ht_news-添加")
    @ApiOperation(value = "ht_news-添加", notes = "ht_news-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HTNews hTNews) {
        hTNewsService.save(hTNews);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param hTNews
     * @return
     */
    @AutoLog(value = "ht_news-编辑")
    @ApiOperation(value = "ht_news-编辑", notes = "ht_news-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HTNews hTNews) {
        hTNewsService.updateById(hTNews);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_news-通过id删除")
    @ApiOperation(value = "ht_news-通过id删除", notes = "ht_news-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        hTNewsService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "ht_news-批量删除")
    @ApiOperation(value = "ht_news-批量删除", notes = "ht_news-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hTNewsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_news-通过id查询")
    @ApiOperation(value = "ht_news-通过id查询", notes = "ht_news-通过id查询")
    @RequestMapping(value = "/queryById", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> queryById(HttpServletRequest req, @RequestParam(name = "id", required = false) String id) {
        if (req.getMethod().equals("POST")) {
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                HTNews hTNews = JSONObject.parseObject(line, HTNews.class);
                id = hTNews.getId();
            }
        }

        HTNews hTNews = hTNewsService.getById(id);
        if (oConvertUtils.isNotEmpty(hTNews) &&
                oConvertUtils.isNotEmpty(hTNews.getCategoryId())) {
			List<Object> list = new ArrayList<Object>();
            String[] categoryIds = hTNews.getCategoryId().split(",");
			if(oConvertUtils.isNotEmpty(categoryIds)){
				for (String categoryId : categoryIds){
					String categoryName = hTNewsCategoryService.getById(categoryId).getName();
					JSONObject info = new JSONObject();
					info.put("categoryId", categoryId);
					info.put("categoryName", categoryName);

					list.add(info);
				}
				hTNews.setCategoryInfos(list.toArray());
			}
        }

        if (oConvertUtils.isEmpty(hTNews)) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hTNews);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param hTNews
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HTNews hTNews) {
        return super.exportXls(request, hTNews, HTNews.class, "ht_news");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HTNews.class);
    }

}
